home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / net / amitcp2_x_gcc.lha / serveraccept.c < prev   
C/C++ Source or Header  |  1994-03-20  |  4KB  |  164 lines

  1. char RCS_ID_SERVERACCEPT_C[] = "$Id: serveraccept.c,v 1.1 1994/03/20 17:11:24 jasegler Exp jasegler $";
  2. /*
  3.  * Author: ppessi <Pekka.Pessi@hut.fi>
  4.  *
  5.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  6.  *                  Helsinki University of Technology, Finland.
  7.  *                  All rights reserved.
  8.  *
  9.  * Created      : Tue May 25 19:30:13 1993 ppessi
  10.  * Last modified: Fri Jun  4 02:26:40 1993 ppessi
  11.  *
  12.  * $Log: serveraccept.c,v $
  13.  * Revision 1.1  1994/03/20  17:11:24  jasegler
  14.  * Initial revision
  15.  *
  16.  * Revision 1.1  1993/06/03  23:27:19  ppessi
  17.  * Cosmetic changes for version 1.0
  18.  *
  19.  */
  20.  
  21. /****** inetdlib.doc/serveraccept *******************************************
  22.  
  23.  *  NAME
  24.  *      serveraccept - Accept a server connection on named port
  25.  *
  26.  *  SYNOPSIS
  27.  *      socket = serveraccept(name, peer);
  28.  *
  29.  *      long serveraccept(char *, struct sockaddr_in *);
  30.  *
  31.  *  DESCRIPTION
  32.  *      The serveraccept() library call binds a socket to the named Internet
  33.  *      TCP port. Then it listens the socket and accepts the connection to
  34.  *      the port. The peer's socket address is returned in sockaddr pointed
  35.  *      by sockaddr argument.
  36.  *
  37.  *      The port name is resolved by getservbyname() call. A numeric value
  38.  *      for port name is also accepted.
  39.  *
  40.  *      This module is meant for daemon developing.
  41.  *
  42.  *  INPUTS
  43.  *      name     - port name or numeric string.
  44.  *      peer     - pointer to struct sockaddr_in
  45.  *
  46.  *  RESULT
  47.  *       socket - positive socket id for success or -1 for failure.
  48.  *
  49.  *       peer   - sockaddr_in structure containing peer's internet address.
  50.  *                Note that on error, the structure containing peer address
  51.  *                is not necessarily updated.
  52.  *
  53.  *  AUTHOR
  54.  *      Pekka Pessi, the AmiTCP/IP Group, <amitcp-group@hut.fi>,
  55.  *      Helsinki University of Technology, Finland.
  56.  *
  57.  *  SEE ALSO
  58.  *       bsdsocket/accept, bsdsocket/getservbyname
  59.  *
  60.  *****************************************************************************
  61.  *
  62.  */
  63.  
  64. #ifdef AMIGA
  65. #if __SASC
  66. #include <proto/socket.h>
  67. #include <proto/dos.h>
  68. #include <clib/exec_protos.h>
  69. #include <pragmas/exec_sysbase_pragmas.h>
  70. #elif __GNUC__
  71. #include <clib/socket_protos.h>
  72. #include <clib/exec_protos.h>
  73. #elif _DCC
  74. #include <clib/socket_protos.h>
  75. #include <clib/exec_protos.h>
  76. #endif
  77. #endif /* AMIGA */
  78.  
  79. #include <errno.h>
  80. #include <netdb.h>
  81.  
  82. #include <sys/param.h>
  83. #include <sys/socket.h>
  84. #include <sys/ioctl.h>
  85. #include <netinet/in.h>
  86.  
  87. #include <signal.h>
  88.  
  89. #include <dos/dos.h>
  90. #include <exec/execbase.h>
  91. #include <dos/var.h>
  92.  
  93. #include <stdlib.h>
  94.  
  95. /*
  96.  * serveraccept:
  97.  *      Accept a server socket from the named port
  98.  */
  99. long
  100. serveraccept (char *pname, struct sockaddr_in *ha)
  101. {
  102.   struct sockaddr_in sin;
  103.   long ha_len = sizeof (*ha);
  104.   int s, sa;
  105.   int port;
  106.   struct servent *sp;
  107.   long on = 1;
  108.  
  109.   /* Create address corresponding our service */
  110.   bzero ((caddr_t) & sin, sizeof (sin));
  111. #ifdef BSD4_4
  112.   sin.sin_len = sizeof (struct sockaddr_in);
  113. #endif
  114.   sin.sin_family = AF_INET;
  115.   /* A port must be in the range 1 - 65535 */
  116.   if ((port = strtoul (pname, NULL, 0)) && port < 65536)
  117.     sin.sin_port = port;
  118.   else if (sp = getservbyname (pname, "tcp"))
  119.     sin.sin_port = sp->s_port;
  120.   else
  121.     {
  122.       return -1;
  123.     }
  124.   sin.sin_addr.s_addr = INADDR_ANY;
  125.  
  126.   if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0)
  127.     {
  128.       PrintNetFault (Errno (), "socket");
  129.       return -1;
  130.     }
  131.  
  132.   /* Reuse this port */
  133.   if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0)
  134.     {
  135.       PrintNetFault (Errno (), "setsockopt");
  136.       sa = -1;
  137.       goto Return;
  138.     }
  139.  
  140.   /* Bind it to socket */
  141.   if (bind (s, (struct sockaddr *) &sin, sizeof (sin)) < 0)
  142.     {
  143.       PrintNetFault (Errno (), "bind");
  144.       sa = -1;
  145.       goto Return;
  146.     }
  147.  
  148.   if (listen (s, 1) < 0)
  149.     {
  150.       PrintNetFault (Errno (), "listen");
  151.       sa = -1;
  152.       goto Return;
  153.     }
  154.  
  155.   if ((sa = accept (s, (struct sockaddr *) ha, &ha_len)) < 0)
  156.     {
  157.       PrintNetFault (Errno (), "accept");
  158.     }
  159.  
  160. Return:
  161.   CloseSocket (s);
  162.   return sa;
  163. }
  164.